package worker;

import Settings.ServerData;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import org.apache.log4j.Logger;

public class TCPMessageProcessor {

    Logger logger = Logger.getLogger(TCPMessageProcessor.class);
    Socket receiverSocket;
    InputStream in;
    OutputStream out;
    byte[] data;
    int available;

    public TCPMessageProcessor(Socket p_Socket) {


        receiverSocket = p_Socket;
        try {
            in = receiverSocket.getInputStream();
            out = receiverSocket.getOutputStream();

        } catch (Exception e) {
            logger.fatal("Exception at obtaining in/out streams " + e);
            return;
        }
        run();
    }

    public void processData() {

        String result = "content_not_matched";
        String receivedStr = new String(data, 0, available);
        boolean sendServerFileContent = false;
        String serverFileContent = null;

        for (int i = 0; i < ServerData.testSettingsObjects.size(); i++) {
            if (ServerData.testSettingsObjects.get(i).fileContentClient != null) {
                if (receivedStr.equals(ServerData.testSettingsObjects.get(i).fileContentClient)) {
                    result = "content_matched";
                    sendServerFileContent = true;
                    serverFileContent = ServerData.testSettingsObjects.get(i).fileContentServer;
                }
            }
        }

        //sending ACK
        try {
            out.write(result.getBytes());
            out.flush();

        } catch (Exception ex) {
            logger.fatal("Exception while sending packet in TCPMessageProcessor", ex);
        }
        try{
        Thread.sleep(5000);  //3 sec sleep
        }catch(Exception ex)
        {
           logger.fatal("interrupted "+ex);
        }
        //sending server file content
        if (sendServerFileContent&&serverFileContent!=null) {
            try {
                out.write(serverFileContent.getBytes());
                out.flush();

            } catch (Exception ex) {
                logger.fatal("Exception while sending packet in TCPMessageProcessor", ex);
            }
        }
    }

    public void readData() {

        //wait 20sec for incoming data
        try {
            for (int i = 0; i < 20; i++) {
                available = in.available();
                if (available > 0) {
                    break;
                }
                Thread.sleep(1000);  //1sec sleep

            }
            data = new byte[available];
            in.read(data);

        } catch (Exception e) {
            logger.fatal("Exception in processing a TCP message");
        }
    }

    public void run() {
        readData();
        processData();
        shutDown();
    }

    public void shutDown() {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
        if (receiverSocket != null) {
            try {
                receiverSocket.close();
                receiverSocket = null;
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
    }
}
